package org.caijuan.transaction.sqlexception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;


@Service
@Slf4j
public class SQLExceptionService {


    @Autowired
    private UserRepository userRepository;

    @Transactional
    public void createUserWrong(String name) throws SQLException {
        UserEntity userEntity = new UserEntity(name);
        userRepository.save(userEntity);
        throw new SQLException("测试SQL");
    }

    @Transactional
    public void createUserRight(String name) {
        UserEntity userEntity = new UserEntity(name);
        userRepository.save(userEntity);
        // 字段超长
        name = "SharedEntityManagerCreator$SharedEntityManagerInvocationHandler:305 ] - Creating new EntityManager for shared EntityManager invocation\n" +
                "[13:54:19.930] [main] [DEBUG] [o.s.o.j.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler:305 ] - Creating new EntityManager";
        userEntity = new UserEntity(name);
        userEntity.setId(91L);
        userRepository.save(userEntity);
    }

    public int getUserCount(String name) {
        return userRepository.findByName(name).size();
    }
}
